//Christian Larco, Assignment 3. Timers and Interrupts
//We use the output of timer 1 and lint it to Timer0. This is used as an external clock
//to trigger the interrupt for the program. 
 
//------------ These are the headers and the libraries used in the program ----------- 
#include <avr/io.h>			//Library header that has the input/output for the AVR
#include <avr/interrupt.h>	//This is the header that has the interrupts for the AVR
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------

//------------ This is the main body of the program ----------------------------------
int count32 = 0;			//This is the counter that catches the falling edge and 
							//counts to 16 

int main(void)				//This is the main body of the program 
{
	//----------- Variables and ports used in this program --------------------------
    DDRB = 0b11111111;		//This command sets the way the LEDS will be used
    DDRD = 0b11000000;		//Enables ports 6 and 7 for D in the program. 					
    PORTB = 0b00000000;		//Clears out port B
    PORTD = 0b00000000;		//Clears out port D
    TCCR1A = 0b00000000;	//Clears out TCCR1A
    TCCR1B = 0b00000011;    //This is the timer for the program 
    TCCR0B = 0b00000110;	//Port reads from the external clock generated from PORTD7					
    EICRA = 0b00000010;		//Command to enable  for the interrupt for falling edge
    TIMSK1 = 1;				//COmmand to enable to catch the overflow interrupt
    sei();					//Command to enable the interrupt. 
	//--------------------------------------------------------------------------------
	
	
	//------------------- While loop that will generate the interrupts ---------------
    while(1)				//This is the infinite while loop that is just going to  
    {						//be waiting for the interrupt to happen
                
    }
	//--------------------------------------------------------------------------------
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------

//---------------------- This is the code that controls the interrupts -----------------
ISR(TIMER1_OVF_vect){
       // From main() and which the wire connects from PD2(Int0) to PD6. PD6 outputs every 10 cycles.
         PORTD ^= 1<<7;		//Command to allow timer1 to recieve the external signal
         count32++;			//Increments the amount of edges that have happened 
         if(count32==32){	//Checks for the amount of edges that have happened it checks
							//32 because since the counter counts both falling and rising edges
							//the number has to be increased by two to get 16 falling edges.
            PORTD ^= 1<<6;  //Command to output to the LED bar
            count32=0;		//Clears the counter to get the next 16 falling edges
         }
PORTB = TCNT0;				//Command to set Timer0 to PORTB.
}
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------